package com.singzi.leetcode.jianzhiOffer2;

import java.util.Arrays;

public class T0075 {
    public static void main(String[] args) {
        int []arr1 = {2,3,1,3,2,4,6,7,9,2,19,33,5,5,6,67,7,8,4,9,0,0,0,0,0,0,34,4,4,4,6};
        int []arr2 = {2,1,4,3,4,9,6};
        Solution0075 solution0075 = new Solution0075();
        System.out.println(Arrays.toString(solution0075.relativeSortArray(arr1, arr2)));
    }
}
class Solution0075 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int []index1 = new int[1001];
        for(int num:arr1){
            index1[num]++;
        }
        int index = 0;
        for(int num:arr2){
            int sum = index1[num];
            while(sum > 0){
                arr1[index] = num;
                sum--;
                index++;
            }
            index1[num] = 0;
        }
        for(int i = 0;i < index1.length;i++){
            int sum = index1[i];
            while (sum > 0){
                arr1[index] = i;
                sum--;
                index++;
            }
        }
        return arr1;
    }
}